//
//  UIECGReviewLocationView.m
//  vhStressECG
//
//  Created by jia yu on 2022/1/7.
//

#import "UIECGReviewLocationView.h"
#import <vhECGTools/vhECGTools.h>

@interface UIECGReviewLocationView(){
    CAShapeLayer *bgLayer;
}

@end

@implementation UIECGReviewLocationView
@synthesize minX;
@synthesize handler;


-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.layer.borderWidth = 2.f;
        self.layer.borderColor = [vhECGTools colorFromHEXString:@"168082"].CGColor;
        self.backgroundColor = [vhECGTools colorFromHEXString:@"047b7c" withAlpha:0.05f];
        self.layer.masksToBounds = YES;
    }
    return self;
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    [bgLayer removeFromSuperlayer];
    bgLayer = nil;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
//    if (nil == bgLayer){
//        CGMutablePathRef path = CGPathCreateMutable();
//        for (float x = 0.f;x<rect.size.width+rect.size.height;x+=12.f){
//            CGPathMoveToPoint(path, NULL, x, 0.f);
//            CGPathAddLineToPoint(path, NULL, 0.f, x);
//        }
//        CGPathMoveToPoint(path, NULL, -1.f, -1.f);
//        CGPathCloseSubpath(path);
//
//        bgLayer = [CAShapeLayer layer];
//        bgLayer.path = path;
//        bgLayer.lineWidth = 1.6f;
//        bgLayer.strokeColor = [UIColor grayColor].CGColor;
//        bgLayer.fillColor = [UIColor clearColor].CGColor;
//        [self.layer addSublayer:bgLayer];
//        CGPathRelease(path);
//    }
    return;
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint currentPoint = [[touches anyObject] locationInView:self];
    CGPoint previousPoint = [[touches anyObject] previousLocationInView:self];
    float seek = currentPoint.x-previousPoint.x;
    float x = self.frame.origin.x + seek;
    if (x <= minX){
        x = minX;
    }
    if (x + self.frame.size.width >= self.superview.frame.size.width){
        x = self.superview.frame.size.width - self.frame.size.width;
    }
    [self setFrame:CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
    
    float move = (self.frame.origin.x - minX)/(self.superview.frame.size.width - self.frame.size.width - minX);
    if (handler){
        handler(move);
    }
    return;
}
@end
